package gaode.com.recentlywebrtcdemo;

import android.content.Context;
import android.media.AudioManager;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.webrtc.MediaStream;
import org.webrtc.RendererCommon;
import org.webrtc.VideoRenderer;
import org.webrtc.VideoRendererGui;


public class ConnectActivity extends AppCompatActivity implements WebRtcClient.RtcListener{
    String TAG1 = "candidate";
    String TAG = "zzw";
    VideoRenderer.Callbacks localRenderer = null;
    VideoRenderer.Callbacks  remoteRenderer = null;
    Button bt;
    Button change;
    EditText name;
    GLSurfaceView videoView;
    AudioManager audioManager;
    WebRtcClient webRtcClient;
    RendererCommon.ScalingType scalingType = RendererCommon.ScalingType.SCALE_ASPECT_FILL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mymain);
        audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        videoView = (GLSurfaceView) findViewById(R.id.glview_call);
        VideoRendererGui.setView(videoView, new Runnable() {
            @Override
            public void run() {
                initClient();
            }
        });
        localRenderer = VideoRendererGui.createGuiRenderer(0, 0, 30, 30, scalingType, true);
        remoteRenderer = VideoRendererGui.createGuiRenderer(30, 0, 50, 50, scalingType, true);
        change = (Button) findViewById(R.id.change);
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    finish();
                //setSpeakerphoneOn(false);
               // VideoRendererGui.update(localRenderer,0, 0, 60, 60, scalingType, true);
  /*              localVideoTrack.removeRenderer(localRenderer);
                videoTrack.removeRenderer(remoteRenderer);
                try {
                    localRenderer = VideoRendererGui.createGui(0, 0, 30, 30, scalingType, true);
                    remoteRenderer = VideoRendererGui.createGui(30, 0, 50, 50, scalingType, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                localVideoTrack.addRenderer(remoteRenderer);
                videoTrack.addRenderer(localRenderer);*/
            }
        });
        name = (EditText) findViewById(R.id.name);
        bt = (Button) super.findViewById(R.id.call);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSpeakerphoneOn(true);
            }
        });
        Button bt1 = (Button) super.findViewById(R.id.add);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //initClient();
            }
        });

    }
    private void initClient(){
         webRtcClient=new WebRtcClient("zzw",this);
    }
    private void setSpeakerphoneOn(boolean on){
        if(on)
        {
            audioManager.setSpeakerphoneOn(true);
        } else
        {
            audioManager.setSpeakerphoneOn(false);//关闭扬声器
            //把声音设定成Earpiece（听筒）出来，设定为正在通话中
            audioManager.setMode(AudioManager.MODE_IN_CALL);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(videoView!=null){
            videoView.onResume();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        if(videoView!=null){
            videoView.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG,"onDestroy");
        webRtcClient.releaseAll();
        super.onDestroy();

    }

    @Override
    public void onStatusChanged(String newStatus) {

    }
    @Override
    public void onLocalStream(MediaStream localStream) {
        localStream.videoTracks.get(0).addRenderer(new VideoRenderer(localRenderer));
    }
    @Override
    public void onAddRemoteStream(final MediaStream remoteStream) {
        remoteStream.videoTracks.get(0).addRenderer(new VideoRenderer(remoteRenderer));
      /*  runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "开始绑定视频");
                if (webRtcClient.peerConnection == null) {
                    Log.i(TAG, "pc == null");
                    return;
                }
                if (remoteStream.videoTracks.size() > 1 || remoteStream.audioTracks.size() > 1) {
                    Log.e(TAG, "size > 1");
                    return;
                }
                Log.i(TAG, "Size()" + remoteStream.videoTracks.size());
                if (remoteStream.videoTracks.size() == 1) {
                    remoteStream.videoTracks.get(0).addRenderer(new VideoRenderer(remoteRenderer));
                }
            }
        });*/
    }

    @Override
    public void onRemoveRemoteStream(MediaStream remoteStream) {
        remoteStream.videoTracks.get(0).dispose();
    }
}
